﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Orgs;
using SmartSpace.Web;

namespace SmartSpace.WebApp.Modules.Orgs.Pages
{
    public partial class NodeType_List : SsTemplatePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindNodeTypeList();

                string parentTypeId = Request.QueryString["ParentTypeId"] ?? String.Empty;
                if (parentTypeId != "")
                {
                    btn_relation.NavigateUrl = "TypeRelation-Add.aspx?ParentTypeId=" + parentTypeId;
                    BindChildNodeTypeList(parentTypeId);
                }
                else {
                    btn_relation.Enabled = false;
                }
            }
        }

        public void BindNodeTypeList()
        {
            OrgsService service = new OrgsService();
            ListResult<NodeType> list = service.GetTypes();

            this.rep1.DataSource = list.Data;
            this.rep1.DataBind();
        }

        public void BindChildNodeTypeList(String nodeTypeId)
        {
            if (nodeTypeId == "")
            {
                return;
            }
            else
            {
                OrgsService service = new OrgsService();
                ListResult<NodeType> list = service.GetSubTypes(new Guid(nodeTypeId));
                this.rep2.DataSource = list.Data;
                this.rep2.DataBind();
            }
        }

        protected void rep1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            OrgsService service = new OrgsService();
            string parentTypeId = Request.QueryString["ParentTypeId"] ?? String.Empty;
            if (e.CommandName == "Delete")
            {
                bool bl = service.DeleteType(e.CommandArgument.ToString());

                if (bl)
                {
                    Response.Redirect("NodeType-List.aspx?ParentTypeId=" + parentTypeId);
                }
            }
        }

        protected void rep2_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            OrgsService service = new OrgsService();
            string parentTypeId = Request.QueryString["ParentTypeId"] ?? String.Empty;
            if (e.CommandName == "Delete")
            {
                bool bl = service.RemoveSubType(new Guid(parentTypeId), new Guid(e.CommandArgument.ToString()));

                if (bl)
                {
                    Response.Redirect("NodeType-List.aspx?ParentTypeId=" + parentTypeId);
                }
            }
        }

        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            string parentTypeId = Request.QueryString["ParentTypeId"] ?? String.Empty;
            if (parentTypeId == "")
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "error", "alert('请选择TypeNode节点');", true);
                return;
            }
            Response.Redirect("TypeRelation-Add.aspx?ParentTypeId=" + parentTypeId);
        }
    }
}